package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Zenglingxiao
* @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
* @createDate 2022-03-04 18:11:06
*/
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService {

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;


    @Autowired
    BaseAttrValueService baseAttrValueService;

    /**
     * 根据id 获取属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoAndValueList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.getAttrInfoAndValueList(category1Id, category2Id, category3Id);
    }

    /**
     * 添加平台属性
     * @param info
     */
    @Override
    public void addsaveAttrInfo(BaseAttrInfo info) {
        Long id = info.getId();
        if (id != null){
            //修改
            revamp(info);
        }else {
            //添加
            add(info);
        }

    }

    //添加
    public void add(BaseAttrInfo info) {

            baseAttrInfoMapper.insert(info);
            Long id = info.getId();
            List<BaseAttrValue> attrValueList = info.getAttrValueList();
            if (attrValueList != null && !attrValueList.isEmpty()) {
                for (BaseAttrValue baseAttrValue : attrValueList) {
                    baseAttrValue.setAttrId(id);
                }
            }
            baseAttrValueService.saveBatch(attrValueList);
    }

    //修改
    public void revamp(BaseAttrInfo info){

            Long id = info.getId();
            //1、跟新属性信息--可能名字修改了
            baseAttrInfoMapper.updateById(info);
            //2、拿到所有需要改变的属性值信息 -- 平台属性值
            List<BaseAttrValue> list = info.getAttrValueList();
            ArrayList<Long> modifyIds = new ArrayList<>();
            for (BaseAttrValue attrValue : list) {
                if (attrValue.getId() != null){
                    modifyIds.add(attrValue.getId());
                }
            }
            //3，前端没有提交过来值，就是要删除的
            if ((list==null || list.size()==0) || (modifyIds == null || modifyIds.size()==0)){
                QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("attr_id",id);
                baseAttrValueService.remove(queryWrapper);
            }

            if (modifyIds.size() > 0){
                baseAttrValueService.remove(new QueryWrapper<BaseAttrValue>()
                        //区间范围
                        .notIn("id",modifyIds)
                        .eq("attr_id",id));
            }else {

            }
            //4、如果前端有带任何id的值就是修改
            for (BaseAttrValue attrValue : list) {
                if (attrValue.getId() != null){
                    //1），属性值若带了id，需要修改
                    baseAttrValueService.updateById(attrValue);

                }else {
                    //2）、如果没带id。新增这个属性的一些值
                    attrValue.setAttrId(id);
                    baseAttrValueService.save(attrValue);
                }
        }
    }

}




